﻿﻿﻿// CALCULATE REMAINING TIME

function getRemainingTimeFromHTML(item)
{
 var remainingTime = new Array();
 remainingTime.Day = $(".remainingDay",$(item)).text();
 remainingTime.Hour = $(".remainingHour",$(item)).text();
 remainingTime.Minute = $(".remainingMinute",$(item)).text();
 remainingTime.Second = $(".remainingSecond",$(item)).text();
 return remainingTime;
}

function updateRemainingTimeToHTML(datetime,item,countdown)
{
 $(".remainingDay",$(item)).text(datetime.Day);
 $(".remainingHour",$(item)).text(datetime.Hour);
 $(".remainingMinute",$(item)).text(datetime.Minute);
 $(".remainingSecond",$(item)).text(datetime.Second);
 if(countdown == true) $(".Text",$(item)).html("Trận đấu sẽ bắt đầu sau ");
 else $(".Text",$(item)).html("Trận đấu đã diễn ra được ");
  if(countdown == true) $(".Text.ShortText",$(item)).html("Còn lại ");
 else $(".Text.ShortText",$(item)).html("Đã diễn ra ");
 var text = "";
 var display = 0;
 if(datetime.Day > 0) 
 {
	display = 1;
	text += datetime.Day + " ngày ";
}
if(datetime.Hour > 0 || display == 1)
{
	display = 1;
	text += datetime.Hour + " giờ ";
}
if(datetime.Minute > 0 || display == 1)
{
	display = 1;
	text+= datetime.Minute + " phút ";
}
if(datetime.Second > 0 || display == 1)
{
	text += datetime.Second + " giây ";
}
 $(".Time",$(item)).html(text);
 if(datetime.Day == 0 && datetime.Hour == 0 && datetime.Minute == 0 && datetime.Second == 0)
 {
  $(item).removeClass("countdown").addClass("countup");
 }
}

function frequentlyUpdateRemainingTime()
{
 $(".remainingTime.countdown").each(function(index,item){
  var remainingTime = getRemainingTimeFromHTML(item);
  remainingTime = subtractDate(remainingTime);
  updateRemainingTimeToHTML(remainingTime,item,true);
 });
 $(".remainingTime.countup").each(function(index,item){
  var remainingTime = getRemainingTimeFromHTML(item);
  remainingTime = addDate(remainingTime);
  updateRemainingTimeToHTML(remainingTime,item,false);
 });
}

function subtractDate(datetime)
{
 var result = new Array();
 result.Day = datetime.Day;
 result.Hour = datetime.Hour;
 result.Minute = datetime.Minute;
 result.Second = datetime.Second;
 result.Second--;
 if(result.Second<0)
 {
  result.Second+=60;
  result.Minute--;
 }
 if(result.Minute < 0)
 {
  result.Minute+=60;
  result.Hour--;
 }
 if(result.Hour < 0)
 {
  result.Hour+=24;
  result.Day--;
 }
 return result;
}

function addDate(datetime)
{
 var result = new Array();
 result.Day = datetime.Day;
 result.Hour = datetime.Hour;
 result.Minute = datetime.Minute;
 result.Second = datetime.Second;
 result.Second++;
 if(result.Second>59)
 {
  result.Second=0;
  result.Minute++;
 }
 if(result.Minute > 59)
 {
  result.Minute=0;
  result.Hour++;
 }
 if(result.Hour >23)
 {
  result.Hour=0;
  result.Day++;
 }
 return result;
}

$(document).ready(function ()
{
	$(".remainingTime").each(function (index,item){
		 $(".NoDisplay",$(item)).hide();
		 $(".remainingSecond",$(item)).text(Math.floor(Math.random()*100)%60);
	});
	window.setInterval("frequentlyUpdateRemainingTime();",1000);
});

$(document).ready(function()
{
	$(".clubinfo a").each(function(index,item){
		$(item).replaceWith($(item).html());
	});
});
